<?php
// +----------------------------
// | Created by PhpStorm
// +----------------------------
// | Author: YnnSon
// +----------------------------
namespace app\api\controller\v1;

use app\api\service\Ocr;
use app\lib\exception\UploadException;
use think\Request;

/**
 * 上传图片和OCR
 * Class Upload
 * @package app\api\controller\v1
 */
class Upload extends BaseController
{
    // 上传图片
    public function image()
    {
        $file = Request::instance()->file('image');
        if (!$file) {
            throw new UploadException([
                'code'      => 400,
                'message'   => '文件上传失败',
                'errorCode' => 50001,
            ]);
        }
        $rule = ['ext' => 'jpg,png,gif'];
        // 移动到框架应用根目录/public/upload/api/image/ 目录下
        $info = $file->validate($rule)->move(ROOT_PATH . 'public' . DS . 'upload' . DS . 'api' . DS . 'image');
        if (!$info) {
            // 上传失败返回错误信息
            throw new UploadException([
                'message' => $file->getError(),
            ]);
        }
        // 成功上传后, 返回上传信息
        $image = Request::instance()->domain() . DS . 'upload' . DS . 'api' . DS . 'image' . DS . $info->getSaveName();
        $path = str_replace('\\', '/', $info->getSaveName());
        $allPath = str_replace('\\', '/', $image);
        return [
            'path'    => $path,
            'allPath' => $allPath,
        ];
    }

    /**
     * @Desc 行驶证OCR
     * @return array
     */
    public function drivingOcr()
    {
        $param = Request::instance()->post();
        return Ocr::drivingOcr($param);
    }
}